
#include <nss.h>
#include <pwd.h>
#include <shadow.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<signal.h>
#define MIN_UID_NUMBER   500
#define MIN_GID_NUMBER   500
#define MACH_NAME "/etc/hostname"
#include<iostream>
using namespace std;

struct Machnamestruct
{
    int  Enable;
    char Machname[100];
};
//返回本机名
struct Machnamestruct
readMachname()
{
    struct Machnamestruct Mach;
    memset(&Mach,0x00,sizeof(struct Machnamestruct));
    FILE *fd;
	char buff[100];
	if ((fd = fopen(MACH_NAME, "r")) == NULL )
	{
        Mach.Enable=0;
	}
	//读取机器名字到buff
    if (fgets (buff, BUFSIZ, fd) == NULL)
    {
		fclose(fd);
        Mach.Enable=0;
	}
	Mach.Enable=1;
	strcpy(Mach.Machname,buff);
	return Mach;

}
//请求数据格式
 struct RequInfo
      {
        char RequInfo[100];
        char Param[100];
        char Machname[100];
      };

//buffer 管理
static char *
get_static(char **buffer, size_t *buflen)
{
	char *result;

	/* Error check.  We return false if things aren't set up right, or
         * there isn't enough buffer space left. */

	if ((buffer == NULL) || (buflen == NULL) ) {
		return NULL;
	}

	/* Return an index into the static buffer */
   // unsigned char *n;
    //strcpy(n,*buffer);
	result = *buffer+1;
	*buffer += **buffer;//*n;

	return result;
}

int main()
{
    struct passwd *p;
    char *buffer;
    size_t buflen=1000;
    signal(SIGPIPE,SIG_IGN);
    struct RequInfo Req;//请求参数的结构体
    memset(&Req,0x00,sizeof(struct RequInfo));
    strcpy(Req.RequInfo,"USER");
    strcpy(Req.Param,"test1");
    struct Machnamestruct Resu=readMachname();
    strcpy(Req.Machname,Resu.Machname);
    //socket 通信
    struct sockaddr_in stSockAddr;
    int Res;
    //套接字描述
    int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (-1 == SocketFD) return NSS_STATUS_TRYAGAIN;
    //清空
    memset(&stSockAddr, 0, sizeof(struct sockaddr_in));

    stSockAddr.sin_family = AF_INET;
    stSockAddr.sin_port = htons(5555);//服务器端口号
    Res = inet_pton(AF_INET, "211.87.230.12", &stSockAddr.sin_addr);//服务器IP地址（转换为网络字节）

    if (0 >= Res)
        return NSS_STATUS_TRYAGAIN;
    if (-1 == connect(SocketFD, (const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)))
    {
        return NSS_STATUS_TRYAGAIN;
    }
    //发送请求的参数到服务端
    send(SocketFD,&Req,sizeof(Req),0);
    memset(buffer,0x00,sizeof(buflen));

    //if(recv(SocketFD,&buff,sizeof(pbuff),0)<0)
       // return NSS_STATUS_TRYAGAIN;
    //拷贝数据

    recv(SocketFD,buffer,1,0);
    recv(SocketFD,buffer+1,*buffer,0);
    p->pw_name=buffer+1;
    char tem[100]="testusdlfjsfs";
    p->pw_name=tem;
    cout<<p.pw_name<<endl;
    /*
    p->pw_name = get_static(&buffer, &buflen);

    p->pw_passwd=get_static(&buffer, &buflen);
    p->pw_uid=*get_static(&buffer, &buflen);
    p->pw_gid=*get_static(&buffer, &buflen);
    p->pw_gecos=get_static(&buffer, &buflen);
    p->pw_dir=get_static(&buffer, &buflen);
    p->pw_shell=get_static(&buffer, &buflen);
    send(SocketFD,&buffer,sizeof(buffer),0);*/
return 0;





}




